package com.topone.service.impl;

import com.topone.Enum.ResultEnum;
import com.topone.Exception.FanZoneException;
import com.topone.dataObject.Mapper.UserMapper;
import com.topone.dataObject.TcUser;
import com.topone.dto.TcUserDTO;
import com.topone.repository.TcUserRepository;
import com.topone.service.TcUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/6,0006.
 */
@Service
public class TcUserServiceImpl implements TcUserService {

    @Autowired
    private TcUserRepository repository;
    @Autowired
    private UserMapper userMapper;

    public  TcUser findOne(String userId){
        TcUser tcUser=new TcUser();
        if(!("".equals(userId))&&userId.isEmpty()){
            tcUser=repository.findOne(userId);
        }
        return tcUser;
    }

    public TcUser findUserInfo(String userName){
        TcUser tcUser=new TcUser();
        tcUser=repository.findByUserName(userName);
        return tcUser;
    }

    @Override
    public TcUser save(TcUser tcUser) {
        return repository.save(tcUser);
    }

    @Override
    public TcUser findByopenid(String openid) {
        return repository.findByOpenid(openid);
    }

    @Override
    public TcUserDTO findUserInfoByopenid(String openid) {
        TcUserDTO tcUser=new TcUserDTO();
        try {
           tcUser=userMapper.findUserInfo(openid);
        }catch (FanZoneException e){
            throw new FanZoneException(ResultEnum.USER_ERROR);
        }
        return tcUser;
    }

    @Override
    public Integer insertUserInfo(TcUserDTO tcUserDTO) {
         return userMapper.insertUserInfo(tcUserDTO);
    }

    @Override
    public Integer updateUserInfo(TcUserDTO tcUserDTO) {
        return userMapper.updateUserInfo(tcUserDTO);
    }

    @Override
    public TcUserDTO findUserByOpenid(String openid) {
        return userMapper.findUserInfoByOpenid(openid);
    }
}
